package com.inspector.app.di.location.gnss;


import android.annotation.SuppressLint;
import android.location.GnssStatus;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.Iterator;

/**
 * 监听卫星数
 */
public class GnssStatusInstance {

    private String TAG = GnssStatusInstance.class.getSimpleName();

    private LocationManager locationManager;
    private final int[] satelliteArr = new int[]{ 0, 0};
    @NonNull
    public int[] getSatelliteArr() {
        return satelliteArr;
    }

    //获取信噪比不为0的卫星数
    public int getAvailableSatellites() {
        return satelliteArr[0];
    }

    //[获取全部卫星数]
    public int getAllSatellites() {
        return satelliteArr[1];
    }

    @RequiresApi(Build.VERSION_CODES.N)
    private final GnssStatus.Callback mGnssStatusCallback = new GnssStatus.Callback() {
        @Override
        public void onStarted() {
            super.onStarted();
//            LogUtil.e(TAG, "onSatelliteStatusChanged onStarted:");
        }

        @Override
        public void onStopped() {
            super.onStopped();
//            LogUtil.e(TAG, "onSatelliteStatusChanged onStopped:");
        }

        @Override
        public void onFirstFix(int ttffMillis) {
            super.onFirstFix(ttffMillis);
        }

        @Override
        public void onSatelliteStatusChanged(GnssStatus status) {
            super.onSatelliteStatusChanged(status);
            try {
//                LogUtil.e(TAG,"onSatelliteStatusChanged status:" + status);
                int satelliteCount = status.getSatelliteCount();
                int count = 0;
                for (int index = 0; index < satelliteCount; index++) {
                    // 每个卫星的载波噪声密度
                    float cn0DbHz = status.getCn0DbHz(index);
                    if (cn0DbHz != 0) {
                        count++;
                    }
                }
                satelliteArr[0] = count;
                satelliteArr[1] = satelliteCount;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    @SuppressLint("MissingPermission")
    private final GpsStatus.Listener mGpsStatusListener = new GpsStatus.Listener() {
        @Override
        public void onGpsStatusChanged(int event) {
            try {
//                LogUtil.e(TAG,"onGpsStatusChanged event:" + event);
                if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                    GpsStatus gpsStatus = locationManager.getGpsStatus(null);
                    //获取卫星颗数的默认最大值
                    int maxSatellites = gpsStatus.getMaxSatellites();
                    //获取所有的卫星
                    Iterator<GpsSatellite> iterator = gpsStatus.getSatellites().iterator();
                    //卫星颗数统计
                    int count = 0;
                    int total = 0;
                    while (iterator.hasNext() && count <= maxSatellites) {
                        GpsSatellite s = iterator.next();
                        //卫星的信噪比
                        float snr = s.getSnr();
                        if (snr != 0) {
                            count++;
                        }
                        total++;
                    }
                    satelliteArr[0] = count;
                    satelliteArr[1] = total;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    @SuppressLint("MissingPermission")
    public void registerGnssStatusCallback(LocationManager locationManager) {
        this.locationManager = locationManager;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locationManager.registerGnssStatusCallback(mGnssStatusCallback);
        } else {
            locationManager.addGpsStatusListener(mGpsStatusListener);
        }
    }

    public void unregisterGnssStatusCallback() {
        if (locationManager == null) return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locationManager.unregisterGnssStatusCallback(mGnssStatusCallback);
        } else {
            locationManager.removeGpsStatusListener(mGpsStatusListener);
        }
    }


    private volatile static GnssStatusInstance instance;

    public static GnssStatusInstance getInstance() {
        if (instance == null) {
            synchronized (GnssStatusInstance.class) {
                if (instance == null) {
                    instance = new GnssStatusInstance();
                }
            }
        }
        return instance;
    }

}
